package com.token.sysflow.core;

import com.alibaba.fastjson.JSONObject;
import com.token.sql.IntegrateSql;
import com.token.sysflow.enumerate.ApprovalTypeEnum;
import com.token.sysflow.enumerate.FlowStatusEnum;
import com.token.sysflow.enumerate.StepTypeEnum;
import com.token.sysflow.exception.ErrorCodeEnum;
import com.token.sysflow.exception.FlowException;
import com.token.sysflow.hander.ApproveProcess;
import com.token.sysflow.hander.FlowRecord;

import java.util.Objects;

/**
 * 子流程提交
 *
 * @author chenjianxiong 2020/4/13
 */
public class SubmitSub extends SubmitFlow{
    private final ApproveProcess mainProcess;

    public SubmitSub(IntegrateSql f, ApproveProcess subProcess,ApproveProcess mainProcess) throws Exception {
        super(f, subProcess);
        this.mainProcess = mainProcess;
        if(subProcess.getApprovalType() != ApprovalTypeEnum.SP.getId()){
            throw new Exception("目前子流程只支持普通环节的审批！");
        }
    }

    @Override
    protected void submitToEnd() throws Exception {

        synchronized (SubmitSub.class) {
            //更新审批数据
            super.updateApproveProcess(submitFlow, FlowStatusEnum.END);

            //把上一环节的多余的办理人设置为停止。
            approveProcess.updateApproverIngToStop(fromStep.getStepId());

            long num = mainProcess.getFlowSubApprovesNum();
            if (num == 0) { //最后一位
                approveProcess.close();

                //把子审批设置成主审批
                super.approveProcess = mainProcess;

                //设置目标流程
                super.setToFlowById(mainProcess.getFlowId());

                //设置目标环节
                JSONObject jsonObject  = submitFlow.getFieldValues();
                if(jsonObject == null){
                    jsonObject = new JSONObject();
                }
                jsonObject.putAll(this.getUserInfoMap());
                super.setToStep(mainProcess.getStep().getDefaultNextStep(jsonObject));

                //计算出目标办理人
                super.executeDefaultApprover();

                //根据环节类型来提交。
                super.submitByToStepType(submitFlow.getActionUser());

                //System.out.println("最后的子流程:后台计算目标环节和办理人--->" + toStep.getStepName() + Arrays.toString(submitFlow.getSelectApprover()));
            }else{
                //提交到结束了，返回下一子流程
                approveProcess.appendFlowProcessInfoNext();
            }
        }
    }

    @Override
    public void submitByToStepType(String actionUserId) throws Exception{
        //把退回标识去掉。
        approveProcess.setReturnAction(false);

        //根据目标环节类型来处理。
        StepTypeEnum type = StepTypeEnum.getStepTypeByValue(super.getToStep().getStepType());
        switch (Objects.requireNonNull(type)){
            case STEPTYPE_TASK:
                submitToTask();
                break;
            case STEPTYPE_END:
                submitToEnd();
                break;
            case STEPTYPE_TASK_HQ:
                submitToHQ();
                break;
            case STEPTYPE_TASK_HS:
                submitToHS();
                break;
            default:
                throw new FlowException(ErrorCodeEnum.STEPTYPE_ERROR);
        }

        handleFlowRecord(actionUserId);


        //生成新的流转记录和办理意见[主流程]
        super.createFlowApproveLog(submitFlow,mainProcess);


        //更新主流程办理人状态结束
        synchronized (SubmitSub.class) {
            mainProcess.updateApproverToEnd(mainProcess.getStepId(), submitFlow.getActionUser(), submitFlow.getOpinions(), submitFlow.getHSResult());
        }

        //写入主流程的FlowRecord
        FlowRecord mainFlowRecord = new FlowRecord(this.getISql(),mainProcess.getKeyUnid());
        mainFlowRecord.appendFlowRecord(fromStep,submitFlow.getActionUser(), mainProcess.getFlowType(),submitFlow.getActionTimer());
        mainFlowRecord.close();
    }
}
